package com.ys.peony.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.ys.peony.R;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseFragment;
import com.ys.peony.enums.SwipeDirect;
import com.ys.peony.model.UserModel;
import com.ys.peony.ui.activity.ClipHeadPhotoActivity;
import com.ys.peony.ui.activity.MainActivity;
import com.ys.peony.ui.activity.SettingActivity;
import com.ys.peony.utils.SharePreferencesUtils;
import com.ys.peony.utils.ToastUtils;

import java.io.File;

import butterknife.BindView;

import static com.squareup.picasso.MemoryPolicy.NO_CACHE;
import static com.squareup.picasso.MemoryPolicy.NO_STORE;

/**
 * describe:我的
 * author:czhang
 * date:2017/5/21 18:53
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.tv_name)
    TextView accountTV;
    @BindView(R.id.tv_role)
    TextView roleTV;

    @BindView(R.id.iv_header)
    ImageView imageViewHeader;

    //设置
    @BindView(R.id.relativeLayout_set)
    RelativeLayout relativeLayoutSet;

    //我的客服
    @BindView(R.id.relativeLayout_service)
    RelativeLayout relativeLayoutService;

    //我的二维码
    @BindView(R.id.relativeLayout_QRcode)
    RelativeLayout relativeLayoutQRcode;

    //帮助
    @BindView(R.id.relativeLayout_help)
    RelativeLayout relativeLayoutHelp;

    @Override
    public int getLayoutRes() {
        return R.layout.mine_fragment;
    }

    @Override
    public void initView() {
        imageViewHeader.setOnClickListener(this);
        relativeLayoutSet.setOnClickListener(this);
        relativeLayoutService.setOnClickListener(this);
        relativeLayoutQRcode.setOnClickListener(this);
        relativeLayoutHelp.setOnClickListener(this);

        UserModel userModel = SharePreferencesUtils.getInstance().getUserModel();
        if(userModel != null) {
            accountTV.setText(userModel.getAccount());
            roleTV.setText(userModel.getName());
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_header://个人信息
                Intent it = new Intent(getActivity(), ClipHeadPhotoActivity.class);
                startActivityForResult(it, YSConstant.ACTIVITY_TAKE_USER_HEADIMG);
                break;
            case R.id.relativeLayout_service://我的客服
                ToastUtils.showToast(getResources().getString(R.string.function_tip));
                break;
            case R.id.relativeLayout_QRcode://我的二维码
                ToastUtils.showToast(getResources().getString(R.string.function_tip));
                break;
            case R.id.relativeLayout_help://帮助
                ToastUtils.showToast(getResources().getString(R.string.function_tip));
                break;
            case R.id.relativeLayout_set://设置
                ((MainActivity) getActivity()).skipToTargetPage(SettingActivity.class, null, SwipeDirect.DIRECT_LEFT);
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == YSConstant.ACTIVITY_TAKE_USER_HEADIMG && resultCode == Activity.RESULT_OK) {
            //TODO 更新图片
            String headImgPath = data.getStringExtra("headImgPath");
            Picasso.with(getActivity()).load(new File(headImgPath))
                    .resize(160, 160)
                    .centerInside()
                    .memoryPolicy(NO_CACHE, NO_STORE)
                    .into(imageViewHeader);
        }
    }
}
